/**
 * Copyright 2015 Qiyi Inc. All rights reserved.
 *
 * @file:   Zhibo.Task.js
 * @path:   js-src/album/
 * @author: karkarrotxu@qiyi.com
 * @date:   2016-03-01
 */
qui(
    function (require, exports, module) {
        Zhibo.Task = $.extend({}, EpgWeb.Task, {
            getLogType:function(taskName){
                var self = this;
                return self.getConfigType(taskName)
            },
            getSourceIp: function (username, regName) {
                var self = this;
                var handler = function (data) {
                    var parent1JDom = $("select[name=server_region]");
                    var parent2JDom = $("select[name=server_region2]");
                    var hasDataRegion = [];
                    var auth_region_arr = [Zhibo.Config.AUTH_REGION_CQ, Zhibo.Config.AUTH_REGION_SH, Zhibo.Config.AUTH_REGION_BJ , Zhibo.Config.AUTH_REGION_TW];

                    if (Zhibo.Config.AUTH_REGION_CQ && Zhibo.Config.AUTH_REGION_SH && Zhibo.Config.AUTH_REGION_BJ&& Zhibo.Config.AUTH_REGION_TW) {
                        $("<option>", {value: "all", html: "全部"}).appendTo(parent1JDom);
                    }

                    if (data != null && data.response != null && data.response.list) {
                        $.each(data.response.list, function (index, obj) {
                            if (hasDataRegion.indexOf(obj[1]) == -1) {
                                hasDataRegion.push(obj[1]);
                            }
                            Zhibo.Config.IP_LINK_AREA[obj[0]] = obj[0] + "[" + Zhibo.Config.REGION_LIST[obj[1]] + "]";
                        });
                        $.each(hasDataRegion, function (i, element) {
                            if (auth_region_arr[element]) {
                                $("<option>", {
                                    value: element,
                                    html: Zhibo.Config.REGION_CH_LIST[element]
                                }).appendTo(parent1JDom);
                                $("<option>", {
                                    value: element,
                                    html: Zhibo.Config.REGION_CH_LIST[element]
                                }).appendTo(parent2JDom);
                            }
                        })
                    } else {
                        console.log("wait to do");
                    }
                }
                var requestData = {url: Zhibo.urlConfig.machinelistUrl, type: "get",
                    params: {type: 0, area: 0, username: username, _: new Date().getTime(), from_client: true }
                };

                return {
                    regName: regName,
                    options: {
                        type: "post",
                        url: EpgWeb.urlConfig.redirect,
                        dataType: "json",
                        data: {"request": JSON.stringify(requestData), timeSign: new Date().getTime()},
                        success: function (data) {
                            handler.call(this, data);
                        },
                        error: function (data) {
                            handler.call(this, data);
                        }
                    }
                }
            }
        })
    }
);